import 'package:example/datebase/text_data_dto.dart';
import 'package:flutter/material.dart';

import 'led_view_painter.dart';

class LedViewWidget extends StatefulWidget {
  final int mode;
  final List<TextDataDTO> textList;
  final Color externalColor;
  const LedViewWidget({Key? key,required this.textList,required this.externalColor, required this.mode}) : super(key: key);

  @override
  State<LedViewWidget> createState() => _LedViewWidgetState();
}

class _LedViewWidgetState extends State<LedViewWidget> {
  List<TextDataDTO> textList = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return widget.textList.isEmpty?const SizedBox():Container(
        alignment: Alignment.center,
        decoration:   BoxDecoration(
          border: Border.all(
            width: 5,
            color: Colors.cyanAccent,
          ),
          borderRadius: BorderRadius.circular(4),
          gradient:widget.mode==0?const LinearGradient(
            begin: Alignment.centerLeft,
            end: Alignment.centerRight,
            colors: [
              Color(0xFFFF0000),
              Color(0xFFFFFF00),
              Color(0xFF00FF00),
              Color(0xFF00FFFF),
              Color(0xFF0000FF),
              Color(0xFFFF00FF),
              Color(0xFFFF0000),
            ],
          )
              : widget.mode == 1? const RadialGradient(
            colors: [
              Color(0xFFFF0000),
              Color(0xFFFFFF00),
              Color(0xFF00FF00),
              Color(0xFF00FFFF),
              Color(0xFF0000FF),
              Color(0xFFFF00FF),
              Color(0xFFFF0000),
            ],
          )
              : const SweepGradient(
            startAngle: 0,
            endAngle: 3.1415926 * 2,
            colors: [
              Color(0xFFFF0000),
              Color(0xFFFFFF00),
              Color(0xFF00FF00),
              Color(0xFF00FFFF),
              Color(0xFF0000FF),
              Color(0xFFFF00FF),
              Color(0xFFFF0000),
            ],
          ),
        ),
      child: ListView.builder(
        itemCount: widget.textList.length,
          scrollDirection: Axis.horizontal,
          physics: const BouncingScrollPhysics(),
          itemBuilder: (BuildContext context,int index){
        return LayoutBuilder(
          builder: (context,cons) {
            double width = (cons.maxHeight/12)*widget.textList[index].widthCount!;
            double height = cons.maxHeight;
            return CustomPaint(
              size:  Size(width, height),
              painter: LedViewPainter(
                text: widget.textList[index],
                externalColor: widget.externalColor,
                canvasSize:  Offset(width, height),
              ),
            );
          }
        );
      })
    );
  }
}
